using System;//For strings and things
using System.IO;//to work with files
using System.Text.RegularExpressions;//to parse the file
using System.Data;//to generate a DataSet
using System.Collections;

namespace DOF
{
	/// <summary>
	/// Converts a file to a DataSet
	/// 
	/// Created by John Cardinal Feb 26th 2003
	/// Use at your own risk; this code does not have any 
	/// error handling to simplify the example
	/// mailto:johnc@ayanova.com
	/// 
	/// Note: Any part of this code that is not generic (i.e. is specific to W3C common log file format)
	/// is marked with the following comment:
	/// //W3C SPECIFIC
	/// This will allow anyone using this class to quickly find the bits that 
	/// will affect other implementations and change as required
	/// </summary>
	public class Asc2ArrString
	{
		private static StreamReader stream;
		private static String val;
        private static string[] result;          
		public string[] Convert(string sLogFile)
		{
            ArrayList tmpArray = new ArrayList();
            if(!System.IO.File.Exists(sLogFile))
				return null;//fail file not found
			
			//Create a new stream for the input file
			stream = new StreamReader(sLogFile, true);			
			// Get the first line of data from the input (log) file.
			val = NextLine(stream);
            
			//string sPattern = "(2B|2E|2I)";            //a row has this pattern.
            string sPattern = "NumOfMsg";            //a row has this pattern.            
			while(val != null) //val is null when the input stream has been read to the end
			{
                if (Regex.IsMatch(val, sPattern, RegexOptions.IgnoreCase))
                {
                    tmpArray.Add(val);                    
                }
				//Get the next line from the input file
				//val = NextLine(stream);
                val = stream.ReadLine();

				//And back to the top....
			}
            stream.Close();
            if (tmpArray.Count > 0)
            {
                result = new string[tmpArray.Count];
                for (int i = 0; i < tmpArray.Count; i++)
                {
                    result[i] = tmpArray[i].ToString();
                }
            }
            else result = null;
            
			return result;
		}




		//Extrace one line of text at a time from an input stream
		//Assumption: each line ends in a single \n character
		private static String NextLine(StreamReader stream)
		{
			
            int stemp = stream.Read();
			String sReturn = "";
						
			while(stemp != -1 && stemp!='\n')//W3C SPECIFIC
			{
				//Convert [ and ] to " because it simplifies 
				//the regular expression for parsing and one of the input 
				//fields (date) is surrounded by them
				//You could remove this if parsing a simple csv file
				//that has nothing but quotes as string delimiters
				//or add your own as required.
				//W3C SPECIFIC
				//if(stemp=='[') stemp='\"';    TD.
				//if(stemp==']') stemp='\"';    TD.

				sReturn += (char)stemp;
				stemp = stream.Read();
			}
			return sReturn;
		}
	}
}
